
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>[k8s] 第四章 kubectl 命令行 实战入门</title>
            </head>
            <body>
            <a href="https://andyoung.blog.csdn.net">原作者博客</a>
            <div id="content_views" class="markdown_views prism-atom-one-light">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <blockquote> 
 <p>本章节将介绍如何在kubernetes集群中部署一个nginx服务，并且能够对其进行访问。</p> 
</blockquote> 
<p>Kubernetes 是一个由主节点和工作节点组成的容器编排工具。它只允许通过作为控制平面核心组件的 API 服务器进行通信。API 服务器公开了一个 HTTP REST API，允许内部组件（如用户和集群）和外部组件之间的通信。</p> 
<p>你可以将 API 服务器视为 Kubernetes 的主要用户界面或前端。它使您能够查询、更新或管理 Kubernetes 对象或资源的状态。为了建立这些交互，Kubernetes API 可以直接发出 REST 请求、使用客户端库或通过kubectl 命令行接收直接命令。</p> 
<p>kubectl 可以帮助您执行各种操作，包括：</p> 
<ul><li>部署容器化应用</li><li>运行 Kubernetes 操作</li><li>监控任务</li><li>检查和管理集群资源</li><li>查看系统日志</li></ul> 
<h2><a id="Kubectl__14"></a>一、Kubectl 概念</h2> 
<p>在开始使用 kubectl 之前，对命令结构有一个基本的了解很重要。以下是命令的一般语法：</p> 
<p><code>kubectl [command] [TYPE] [NAME] [flags］</code></p> 
<p>让我们回顾一下这些属性：</p> 
<ul><li>command — 描述要执行的操作类型。常见操作包括创建、写入、获取、应用 和删除。这些命令要么创建新的 Kubernetes 对象，要么修改现有对象，要么请求有关现有对象的信息。你可以在单个命令中指定多个资源。</li><li>TYPE —描述你的命令所针对的资源类型。常见的选项是pod、 service、 deployment、 daemonset、 statefulset、 job 或cronjob。</li><li>NAME —这是区分大小写的，指定你的命令应该应用到的资源的名称。提供资源名称不是强制性的——如果你提供名称，则命令仅限于该特定资源（或者如果没有该名称的资源，你会收到错误消息。如果不指定，则该命令适用于当前命名空间集群中的所有资源。</li><li>flags —这些表示特殊选项或对特定信息的请求。它们也可以用作修饰符来覆盖默认值或环境变量。</li></ul> 
<pre><code>kubectl get pod -n kube-system
</code></pre> 
<h2><a id="9__kubectl__31"></a>二、9 大 kubectl 命令详解</h2> 
<h3><a id="1_List_Kubernetes_Resources_Kubernetes__33"></a><strong>1. List Kubernetes Resources（列出 Kubernetes 资源）</strong></h3> 
<p>使用 kubectl get 操作列出一个或多个资源。例如，使用<code>kubectl get pods</code>列出所有 <code>Kubernetes pods</code>。添加一个输出标志，如<code>get pods -o wide</code>将列出pod和额外的数据，如他们相关的节点名称。</p> 
<p>get 操作可以列出额外的资源，例如服务和复制控制器。使用 kubectl 命令get rc或get services列出所有服务和复制控制器。</p> 
<p>get操作的不同变体允许你执行诸如指定特定节点和使用短别名减少资源长度等操作。</p> 
<pre><code># kubectl get pod -n kube-system
NAME                              READY   STATUS    RESTARTS       AGE
coredns-78fcd69978-dmqhr          1/1     Running   0              13h
coredns-78fcd69978-zlhdc          1/1     Running   0              13h
etcd-a1                           1/1     Running   2 (14h ago)    16h
kube-apiserver-a1                 1/1     Running   1 (14h ago)    16h
kube-controller-manager-a1        1/1     Running   28 (14h ago)   16h
kube-flannel-ds-dpvzh             1/1     Running   5 (13h ago)    365d
kube-flannel-ds-lsscw             1/1     Running   2 (14h ago)    365d
kube-flannel-ds-r2w4p             1/1     Running   2 (14h ago)    365d
kube-proxy-4bx9g                  1/1     Running   3 (14h ago)    365d
kube-proxy-6fkj5                  1/1     Running   7 (13h ago)    365d
kube-proxy-lkrgh                  1/1     Running   3 (14h ago)    365d
kube-scheduler-a1                 1/1     Running   29 (14h ago)   16h
metrics-server-569bf5b784-jsjlh   1/1     Running   1 (14h ago)    306d
</code></pre> 
<h3><a id="2_Describe_61"></a><strong>2. Describe（描述）</strong></h3> 
<p>get 命令提供了一个紧凑的资源列表，而<code>kubectl describe</code>命令提供了一个或多个资源状态的详细报告。Kubernetes 资源。<code>kubectl describe pods</code>命令描述了所有 <code>Kubernetes pods</code>。如果复制控制器正在管理 pods，您可以使用<code>describe pods</code>命令显示指定控制器的 pods的详细信息。</p> 
<p>kubectl describe操作可以专注于特定的 pod 或节点。例如，你可以使用 kubectl 命令describe nodes来显示指定节点的详细信息。另外，describe pods将显示指定 pod 的详细信息。</p> 
<pre><code># kubectl describe pod etcd-a1 -n kube-system
Name:                 etcd-a1
Namespace:            kube-system
Priority:             2000001000
Priority Class Name:  system-node-critical
Node:                 a1/192.168.0.183
Start Time:           Tue, 15 Nov 2022 20:20:06 +0800
Labels:               component=etcd
                      tier=control-plane
Annotations:          kubeadm.kubernetes.io/etcd.advertise-client-urls: https://192.168.0.183:2379
                      kubernetes.io/config.hash: 8f4b0673d87d5a2a95cce5eaf4ea8b75
                      kubernetes.io/config.mirror: 8f4b0673d87d5a2a95cce5eaf4ea8b75
                      kubernetes.io/config.seen: 2022-11-15T17:28:54.430271584+08:00
                      kubernetes.io/config.source: file
                      seccomp.security.alpha.kubernetes.io/pod: runtime/default
Status:               Running
IP:                   192.168.0.183
IPs:
  IP:           192.168.0.183
Controlled By:  Node/a1
Containers:
...
</code></pre> 
<h3><a id="3_Create_and_Modify__92"></a><strong>3. Create and Modify（创建和修改 ）</strong></h3> 
<p>你可以使用 kubectl 中的apply命令，从特定文件或标准输入 (stdin) 创建资源。<code>kubectl apply -f servicename.yaml</code>命令使用特定的 YAML 文件创建一个新服务。<code>-f</code>标志表示文件的使用。</p> 
<p>例如，如果你想使用 YAML 文件的内容创建一个新的 RC，你可以使用<code>apply -f controllername.yaml</code>命令。另一种选择是使用更广泛的命令（例如apply -f）在指定目录的 JSON 或 YAML 文件中创建服务资源。</p> 
<ul><li>命令式对象管理：直接使用命令去操作kubernetes资源</li></ul> 
<p><code>kubectl run nginx-pod --image=nginx:1.17.1 --port=80</code></p> 
<ul><li>命令式对象配置：通过命令配置和配置文件去操作kubernetes资源</li></ul> 
<p><code>kubectl create/patch -f nginx-pod.yaml</code></p> 
<ul><li>声明式对象配置：通过apply命令和配置文件去操作kubernetes资源</li></ul> 
<p><code>kubectl apply -f nginx-pod.yaml</code></p> 
<h3><a id="4_Delete_110"></a><strong>4. Delete（删除）</strong></h3> 
<p><code>kubectl delete</code>操作会终止你不再需要的服务和资源。它对于管理 Kubernetes 至关重要，允许你为不同的 Kubernetes 任务释放计算能力。</p> 
<p>例如，你可以使用<code>kubectl delete pods –all</code>命令删除所有 pod。删除 pod 时，使用单独的 YAML 文件中指定的资源名称和类型是比较安全的。如果你使用examplepod.yaml文件创建 pod，你可以使用命令<code>delete -f examplepod.yaml</code>删除该 pod 。</p> 
<p>Kubectl还可以删除共享特定标签的服务和 pod，你可以使用标签操作分配这些标签。例如，<code>delete pods,services -l name=</code>命令会删除所有标记为“example-name”的 pod 和服务。</p> 
<p><code>kubectl delete pod nginx-5ff7956ff6-fg2db -n dev</code></p> 
<h3><a id="5_PersistentVolume_PV_120"></a><strong>5. PersistentVolume 持久卷（PV）</strong></h3> 
<p><code>Kubernetes PersistentVolume</code>是一种在 Kubernetes 集群中配置存储的机制。它可以由管理员手动配置，也可以使用 <code>StorageClasses</code> 自动配置。PV 是集群中的独立资源，独立于使用它们的各个 pod。如果一个 pod 出现故障，PV 将保持原位并可以安装在其他 pod 上。</p> 
<p>在幕后，PV 对象使用 NFS、iSCSI 或公共云存储服务与物理存储设备交互。</p> 
<p>以下是可用于处理 PersistentVolume 的三个有用命令：</p> 
<p>运行以下命令在节点上创建 PV（提供 PV 清单的 URL）：<code>kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml</code></p> 
<p>创建一个PersistentVolumeClaim（PVC），请求一个具有特定标准的PV。这可以实现PV的动态配置。运行此命令以在集群中创建PVC：<code>kubectl apply -f https://k8s.io/examples/pods/storage/pv-claim.yaml</code></p> 
<p>创建 PVC 后，Kubernetes 控制平面就开始寻找合适的 PV。当它找到一个，它就把 PVC 绑定到 PV。运行以下命令以查看 PV 的状态：kubectl 获取 pv 任务-pv-volume</p> 
<h3><a id="6_Security_Context_134"></a><strong>6. Security Context（安全环境）</strong></h3> 
<p>在 Kubernetes 中安全地运行工作负载可能是一种挑战。各种设置会影响 Kubernetes API 使用的安全控制。Kubernetes 提供的强大工具之一是设置所有 pod 清单都可以使用的 <code>security Context</code>。</p> 
<p>在 Kubernetes 中使用security Context很容易。您只需在部署 pod 时在部署清单中包含security Context。例如，以下代码块指示 Kubernetes 运行用户 ID 为 1000 且组 ID 为 2000 的 pod：</p> 
<pre><code>Spec:

  securityContext:

    runAsUser: 1000

    fsGroup: 2000
</code></pre> 
<p>与 RBAC 不同，安全上下文不需要你定义不同类型的文件（例如 Roles 和 RoleBindings）来应用安全规则。只需在声明部署时添加所需的安全上下文代码，Kubernetes 就会自动为你应用规则。</p> 
<h3><a id="7_Managing_Deployments_152"></a><strong>7. Managing Deployments（管理部署）</strong></h3> 
<p>有几个有用的命令可用于管理 Kubernetes 部署。部署管理还包括 StatefulSet 和 DaemonSet 管理。</p> 
<p>更新部署、有状态集或守护程序集时，你可以使用rollout status命令查看更新的状态。还可以使用命令<code>rollout undo &lt;statefulset-name / app-name&gt;</code>取消部署。 另一方面，<code>rollout history &lt;deployment name / app name&gt;</code>命令将为你提供部署更改的历史记录。</p> 
<p>这些命令在实践中并不常见，因为大多数管理员使用 Helm 等工具来管理部署。</p> 
<p>你可以使用命令<code>scale –replicas=N &lt;deployment name / app name&gt;</code>来调整为你的部署运行的 pod 数量。在这种情况下，N表示更新的副本数。它产生与使用<code>kubectl edit &lt;deployment name / app name&gt;</code>命令调整副本数量相同的结果。</p> 
<p>因为你可能正在使用 Helm 或 pod 自动缩放器进行静态更改，所以你不太可能在实践中使用这种方法或进行任何手动更改。还可以使用<code>kubectl autoscale</code>操作设置基本的Kubernetes 自动缩放功能。然而，此命令仅适用于 CPU 利用率指标。</p> 
<h3><a id="8Executing_Commands_164"></a>8.<strong>Executing Commands（执行命令）</strong></h3> 
<p>你可以通过kubectl使用exec操作来执行命令，该操作针对容器或 pod 运行命令。例如，你可以使用<code>kubectl exec &lt;pod-name&gt; date</code> 命令在指定 pod 的 shell 中运行date命令并显示输出。默认情况下，此命令在 pod 中的第一个容器上执行。</p> 
<p>再举一个例子，你可以在 pod 中的指定容器中使用 <code>exec-c container-name date</code>命令。</p> 
<h3><a id="9explain__169"></a>9.<strong>explain</strong> 查看资源结构</h3> 
<p>explain用来查看配置文件的 资源结构，如果不知道配置文件中的资源用有哪些结构，那么就可以使用explain命令来查看</p> 
<pre><code># 查看pod资源结构
kubectl explain pod
# 查看容器配置的资源结构
kubectl explain pod.spec.containers

#查看namespace资源结构
kubectl explain namespace

#查看service资源结构
kubectl explain service

# 查看deployment资源结构
kubectl explain deployment
</code></pre> 
<h2><a id="kubectl__187"></a>三、kubectl 资源操作</h2> 
<p>资源记录。获取资源及其字段的文档。</p> 
<pre><code>kubectl explain pods

#获取资源的特定字段的文档
kubectl explain pods.spec.containers
</code></pre> 
<h3><a id="Namespace_196"></a>Namespace</h3> 
<p>Namespace是kubernetes系统中的一种非常重要资源，它的主要作用是用来实现<strong>多套环境的资源隔离</strong>或者<strong>多租户的资源隔离</strong>。</p> 
<p>默认情况下，kubernetes集群中的所有的Pod都是可以相互访问的。但是在实际中，可能不想让两个Pod之间进行互相的访问，那此时就可以将两个Pod划分到不同的namespace下。kubernetes通过将集群内部的资源分配到不同的Namespace中，可以形成逻辑上的"组"，以方便不同的组的资源进行隔离使用和管理。</p> 
<p>可以通过kubernetes的授权机制，将不同的namespace交给不同租户进行管理，这样就实现了多租户的资源隔离。此时还能结合kubernetes的资源配额机制，限定不同租户能占用的资源，例如CPU使用量、内存使用量等等，来实现租户可用资源的管理。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/32e49ba7dfee2de98e9bd74d6e169aa3.png" alt="image-20221116095929630"></p> 
<p>kubernetes在集群启动之后，会默认创建几个namespace</p> 
<pre><code class="prism language-powershell"><span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl  get namespace</span>
NAME              STATUS   AGE
default           Active   45h     <span class="token comment">#  所有未指定Namespace的对象都会被分配在default命名空间</span>
kube-node-lease   Active   45h     <span class="token comment">#  集群节点之间的心跳维护，v1.13开始引入</span>
kube-public       Active   45h     <span class="token comment">#  此命名空间下的资源可以被所有人访问（包括未认证用户）</span>
kube-system       Active   45h     <span class="token comment">#  所有由Kubernetes系统创建的资源都处于这个命名空间</span>
</code></pre> 
<p>下面来看namespace资源的具体操作：</p> 
<p><strong>查看</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 1 查看所有的ns  命令：kubectl get ns</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get ns</span>
NAME              STATUS   AGE
default           Active   45h
kube-node-lease   Active   45h
kube-public       Active   45h     
kube-system       Active   45h     

<span class="token comment"># 2 查看指定的ns   命令：kubectl get ns ns名称</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get ns default</span>
NAME      STATUS   AGE
default   Active   45h

<span class="token comment"># 3 指定输出格式  命令：kubectl get ns ns名称  -o 格式参数</span>
<span class="token comment"># kubernetes支持的格式有很多，比较常见的是wide、json、yaml</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get ns default -o yaml</span>
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: <span class="token string">"2020-04-05T04:44:16Z"</span>
  name: default
  resourceVersion: <span class="token string">"151"</span>
  selfLink: <span class="token operator">/</span>api/v1/namespaces/default
  uid: 7405f73a-e486-43d4-9db6-145f1409f090
spec:
  finalizers:
  <span class="token operator">-</span> kubernetes
status:
  phase: Active
  
<span class="token comment"># 4 查看ns详情  命令：kubectl describe ns ns名称</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe ns default</span>
Name:         default
Labels:       &lt;none&gt;
Annotations:  &lt;none&gt;
Status:       Active  <span class="token comment"># Active 命名空间正在使用中  Terminating 正在删除命名空间</span>

<span class="token comment"># ResourceQuota 针对namespace做的资源限制</span>
<span class="token comment"># LimitRange针对namespace中的每个组件做的资源限制</span>
No resource quota<span class="token punctuation">.</span>
No LimitRange resource<span class="token punctuation">.</span>
</code></pre> 
<p><strong>创建</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 创建namespace</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl create ns dev</span>
namespace/dev created
</code></pre> 
<p><strong>删除</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 删除namespace</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete ns dev</span>
namespace <span class="token string">"dev"</span> deleted
</code></pre> 
<p><strong>配置方式</strong></p> 
<p>首先准备一个yaml文件：ns-dev.yaml</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Namespace
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> dev
</code></pre> 
<p>然后就可以执行对应的创建和删除命令了：</p> 
<pre><code>创建：kubectl  create  -f  ns-dev.yaml

删除：kubectl  delete  -f  ns-dev.yaml
</code></pre> 
<h3><a id="Pod_300"></a>Pod</h3> 
<p>Pod是kubernetes集群进行管理的最小单元，程序要运行必须部署在容器中，而容器必须存在于Pod中。</p> 
<p>Pod可以认为是容器的封装，一个Pod中可以存在一个或者多个容器。<br> <img src="https://i-blog.csdnimg.cn/blog_migrate/e1bfe27932a69f4006c855a0ff43426e.png#pic_center" alt="Pod"></p> 
<p>kubernetes在集群启动之后，集群中的各个组件也都是以Pod方式运行的。可以通过下面命令查看：</p> 
<pre><code class="prism language-powershell"><span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pod -n kube-system</span>
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-6955765f44-68g6v         1/1     Running   0          2d1h
kube-system   coredns-6955765f44-cs5r8         1/1     Running   0          2d1h
kube-system   etcd-master                      1/1     Running   0          2d1h
kube-system   kube-apiserver-master            1/1     Running   0          2d1h
kube-system   kube-controller-manager-master   1/1     Running   0          2d1h
kube-system   kube-flannel-ds-amd64-47r25      1/1     Running   0          2d1h
kube-system   kube-flannel-ds-amd64-ls5lh      1/1     Running   0          2d1h
kube-system   kube-proxy-685tk                 1/1     Running   0          2d1h
kube-system   kube-proxy-87spt                 1/1     Running   0          2d1h
kube-system   kube-scheduler-master            1/1     Running   0          2d1h
</code></pre> 
<p><strong>创建并运行</strong></p> 
<p>kubernetes没有提供单独运行Pod的命令，都是通过Pod控制器来实现的</p> 
<pre><code class="prism language-powershell"><span class="token comment"># 命令格式： kubectl run (pod控制器名称) [参数] </span>
<span class="token comment"># --image  指定Pod的镜像</span>
<span class="token comment"># --port   指定端口</span>
<span class="token comment"># --namespace  指定namespace</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl run nginx --image=nginx:1.17.1 --port=80 --namespace dev </span>
deployment<span class="token punctuation">.</span>apps/nginx created
</code></pre> 
<p><strong>查看pod信息</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 查看Pod基本信息</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods -n dev</span>
NAME                     READY   STATUS    RESTARTS   AGE
nginx-5ff7956ff6-fg2db   1/1     Running   0          43s

<span class="token comment"># 查看Pod的详细信息</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe pod nginx-5ff7956ff6-fg2db -n dev</span>
Name:         nginx-5ff7956ff6-fg2db
Namespace:    dev
Priority:     0
Node:         node1/192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>101
<span class="token function">Start</span> Time:   Wed<span class="token punctuation">,</span> 08 Apr 2020 09:29:24 <span class="token operator">+</span>0800
Labels:       pod-template-hash=5ff7956ff6
              run=nginx
Annotations:  &lt;none&gt;
Status:       Running
IP:           10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>23
IPs:
  IP:           10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>23
Controlled By:  ReplicaSet/nginx-5ff7956ff6
Containers:
  nginx:
    Container ID:   docker:<span class="token operator">/</span><span class="token operator">/</span>4c62b8c0648d2512380f4ffa5da2c99d16e05634979973449c98e9b829f6253c
    Image:          nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>1
    Image ID:       docker-pullable:<span class="token operator">/</span><span class="token operator">/</span>nginx@sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed<span class="token punctuation">,</span> 08 Apr 2020 09:30:01 <span class="token operator">+</span>0800
    Ready:          True
    Restart Count:  0
    Environment:    &lt;none&gt;
    Mounts:
      <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>run/secrets/kubernetes<span class="token punctuation">.</span>io/serviceaccount <span class="token keyword">from</span> default-token-hwvvw <span class="token punctuation">(</span>ro<span class="token punctuation">)</span>
Conditions:
  <span class="token function">Type</span>              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  default-token-hwvvw:
    <span class="token function">Type</span>:        Secret <span class="token punctuation">(</span>a volume populated by a Secret<span class="token punctuation">)</span>
    SecretName:  default-token-hwvvw
    Optional:    false
QoS <span class="token keyword">Class</span>:       BestEffort
Node-Selectors:  &lt;none&gt;
Tolerations:     node<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/not-ready:NoExecute <span class="token keyword">for</span> 300s
                 node<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/unreachable:NoExecute <span class="token keyword">for</span> 300s
Events:
  <span class="token function">Type</span>    Reason     Age        <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>    <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span>       <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal  Scheduled  &lt;unknown&gt;  default-scheduler  Successfully assigned dev/nginx-5ff7956ff6-fg2db to node1
  Normal  Pulling    4m11s      kubelet<span class="token punctuation">,</span> node1     Pulling image <span class="token string">"nginx:1.17.1"</span>
  Normal  Pulled     3m36s      kubelet<span class="token punctuation">,</span> node1     Successfully pulled image <span class="token string">"nginx:1.17.1"</span>
  Normal  Created    3m36s      kubelet<span class="token punctuation">,</span> node1     Created container nginx
  Normal  Started    3m36s      kubelet<span class="token punctuation">,</span> node1     Started container nginx
</code></pre> 
<p><strong>访问Pod</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 获取podIP</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods -n dev -o wide</span>
NAME                     READY   STATUS    RESTARTS   AGE    IP             NODE    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 
nginx-5ff7956ff6-fg2db   1/1     Running   0          190s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>23   node1   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

<span class="token comment">#访问POD</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># curl http://10.244.1.23:80</span>
&lt;<span class="token operator">!</span>DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Welcome to nginx!&lt;<span class="token operator">/</span>title&gt;
&lt;<span class="token operator">/</span>head&gt;
&lt;body&gt;
	&lt;p&gt;&lt;em&gt;Thank you <span class="token keyword">for</span> <span class="token keyword">using</span> nginx<span class="token punctuation">.</span>&lt;<span class="token operator">/</span>em&gt;&lt;<span class="token operator">/</span>p&gt;
&lt;<span class="token operator">/</span>body&gt;
&lt;<span class="token operator">/</span>html&gt;
</code></pre> 
<p><strong>删除指定Pod</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 删除指定Pod</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete pod nginx-5ff7956ff6-fg2db -n dev</span>
pod <span class="token string">"nginx-5ff7956ff6-fg2db"</span> deleted

<span class="token comment"># 此时，显示删除Pod成功，但是再查询，发现又新产生了一个 </span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods -n dev</span>
NAME                     READY   STATUS    RESTARTS   AGE
nginx-5ff7956ff6-jj4ng   1/1     Running   0          21s

<span class="token comment"># 这是因为当前Pod是由Pod控制器创建的，控制器会监控Pod状况，一旦发现Pod死亡，会立即重建</span>
<span class="token comment"># 此时要想删除Pod，必须删除Pod控制器</span>

<span class="token comment"># 先来查询一下当前namespace下的Pod控制器</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get deploy -n  dev</span>
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           9m7s

<span class="token comment"># 接下来，删除此PodPod控制器</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete deploy nginx -n dev</span>
deployment<span class="token punctuation">.</span>apps <span class="token string">"nginx"</span> deleted

<span class="token comment"># 稍等片刻，再查询Pod，发现Pod被删除了</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods -n dev</span>
No resources found in dev namespace<span class="token punctuation">.</span>
</code></pre> 
<p><strong>配置操作</strong></p> 
<p>创建一个pod-nginx.yaml，内容如下：</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>1.17.1
    <span class="token key atrule">name</span><span class="token punctuation">:</span> pod
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>port
      <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
      <span class="token key atrule">protocol</span><span class="token punctuation">:</span> TCP
</code></pre> 
<p>然后就可以执行对应的创建和删除命令了：</p> 
<pre><code>创建：kubectl  create  -f  pod-nginx.yaml

删除：kubectl  delete  -f  pod-nginx.yaml
</code></pre> 
<h3><a id="Label_476"></a>Label</h3> 
<p>Label是kubernetes系统中的一个重要概念。它的作用就是在资源上添加标识，用来对它们进行区分和选择。</p> 
<p>Label的特点：</p> 
<ul><li>一个Label会以key/value键值对的形式附加到各种对象上，如Node、Pod、Service等等</li><li>一个资源对象可以定义任意数量的Label ，同一个Label也可以被添加到任意数量的资源对象上去</li><li>Label通常在资源对象定义时确定，当然也可以在对象创建后动态添加或者删除</li></ul> 
<p>可以通过Label实现资源的多维度分组，以便灵活、方便地进行资源分配、调度、配置、部署等管理工作。</p> 
<blockquote> 
 <p>一些常用的Label 示例如下：</p> 
 <ul><li>版本标签：“version”:“release”, “version”:“stable”…</li><li>环境标签：“environment”:“dev”，“environment”:“test”，“environment”:“pro”</li><li>架构标签：“tier”:“frontend”，“tier”:“backend”</li></ul> 
</blockquote> 
<p>标签定义完毕之后，还要考虑到标签的选择，这就要使用到Label Selector，即：</p> 
<pre><code>Label用于给某个资源对象定义标识

Label Selector用于查询和筛选拥有某些标签的资源对象
</code></pre> 
<p>当前有两种Label Selector：</p> 
<ul><li> <p>基于等式的Label Selector</p> <p>name = slave: 选择所有包含Label中key="name"且value="slave"的对象</p> <p>env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象</p> </li><li> <p>基于集合的Label Selector</p> <p>name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象</p> <p>name not in (frontend): 选择所有包含Label中的key="name"且value不等于"frontend"的对象</p> </li></ul> 
<p>标签的选择条件可以使用多个，此时将多个Label Selector进行组合，使用逗号","进行分隔即可。例如：</p> 
<pre><code>	name=slave，env!=production

	name not in (frontend)，env!=production
</code></pre> 
<p><strong>命令方式</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 为pod资源打标签</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl label pod nginx-pod version=1.0 -n dev</span>
pod/nginx-pod labeled

<span class="token comment"># 为pod资源更新标签</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl label pod nginx-pod version=2.0 -n dev --overwrite</span>
pod/nginx-pod labeled

<span class="token comment"># 查看标签</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pod nginx-pod  -n dev --show-labels</span>
NAME        READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod   1/1     Running   0          10m   version=2<span class="token punctuation">.</span>0

<span class="token comment"># 筛选标签</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pod -n dev -l version=2.0  --show-labels</span>
NAME        READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod   1/1     Running   0          17m   version=2<span class="token punctuation">.</span>0
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pod -n dev -l version!=2.0 --show-labels</span>
No resources found in dev namespace<span class="token punctuation">.</span>

<span class="token comment">#删除标签</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl label pod nginx-pod version- -n dev</span>
pod/nginx-pod labeled
</code></pre> 
<p><strong>配置方式</strong></p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">"3.0"</span> 
    <span class="token key atrule">env</span><span class="token punctuation">:</span> <span class="token string">"test"</span>
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>1.17.1
    <span class="token key atrule">name</span><span class="token punctuation">:</span> pod
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>port
      <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
      <span class="token key atrule">protocol</span><span class="token punctuation">:</span> TCP
</code></pre> 
<p>然后就可以执行对应的更新命令了：kubectl apply -f pod-nginx.yaml</p> 
<h3><a id="Deployment_571"></a>Deployment</h3> 
<p>在kubernetes中，Pod是最小的控制单元，但是kubernetes很少直接控制Pod，一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理，确保pod资源符合预期的状态，当pod的资源出现故障时，会尝试进行重启或重建pod。</p> 
<p>在kubernetes中Pod控制器的种类有很多，本章节只介绍一种：Deployment。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/d629626df44fe86b6c5db94ed519e117.png" alt="image-20221116100937682"></p> 
<p><strong>命令操作</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 命令格式: kubectl run deployment名称  [参数] </span>
<span class="token comment"># --image  指定pod的镜像</span>
<span class="token comment"># --port   指定端口</span>
<span class="token comment"># --replicas  指定创建pod数量</span>
<span class="token comment"># --namespace  指定namespace</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl run nginx --image=nginx:1.17.1 --port=80 --replicas=3 -n dev</span>
deployment<span class="token punctuation">.</span>apps/nginx created

<span class="token comment"># 查看创建的Pod</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get pods -n dev</span>
NAME                     READY   STATUS    RESTARTS   AGE
nginx-5ff7956ff6-6k8cb   1/1     Running   0          19s
nginx-5ff7956ff6-jxfjt   1/1     Running   0          19s
nginx-5ff7956ff6-v6jqw   1/1     Running   0          19s

<span class="token comment"># 查看deployment的信息</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get deploy -n dev</span>
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   3/3     3            3           2m42s

<span class="token comment"># UP-TO-DATE：成功升级的副本数量</span>
<span class="token comment"># AVAILABLE：可用副本的数量</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get deploy -n dev -o wide</span>
NAME    READY UP-TO-DATE  AVAILABLE   AGE     CONTAINERS   IMAGES              SELECTOR
nginx   3/3     3         3           2m51s   nginx        nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>1        run=nginx

<span class="token comment"># 查看deployment的详细信息</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl describe deploy nginx -n dev</span>
Name:                   nginx
Namespace:              dev
CreationTimestamp:      Wed<span class="token punctuation">,</span> 08 Apr 2020 11:14:14 <span class="token operator">+</span>0800
Labels:                 run=nginx
Annotations:            deployment<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/revision: 1
Selector:               run=nginx
Replicas:               3 desired <span class="token punctuation">|</span> 3 updated <span class="token punctuation">|</span> 3 total <span class="token punctuation">|</span> 3 available <span class="token punctuation">|</span> 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable<span class="token punctuation">,</span> 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  &lt;none&gt;
    Mounts:       &lt;none&gt;
  Volumes:        &lt;none&gt;
Conditions:
  <span class="token function">Type</span>           Status  Reason
  <span class="token operator">--</span><span class="token operator">--</span>           <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>  <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  &lt;none&gt;
NewReplicaSet:   nginx-5ff7956ff6 <span class="token punctuation">(</span>3/3 replicas created<span class="token punctuation">)</span>
Events:
  <span class="token function">Type</span>    Reason             Age    <span class="token keyword">From</span>                   Message
  <span class="token operator">--</span><span class="token operator">--</span>    <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>             <span class="token operator">--</span><span class="token operator">--</span>   <span class="token operator">--</span><span class="token operator">--</span>                   <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal  ScalingReplicaSet  5m43s  deployment-controller  Scaled up replicaset nginx-5ff7956ff6 to 3
  
<span class="token comment"># 删除 </span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete deploy nginx -n dev</span>
deployment<span class="token punctuation">.</span>apps <span class="token string">"nginx"</span> deleted
</code></pre> 
<p><strong>配置操作</strong></p> 
<p>创建一个deploy-nginx.yaml，内容如下：</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">replicas</span><span class="token punctuation">:</span> <span class="token number">3</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">run</span><span class="token punctuation">:</span> nginx
  <span class="token comment">## pod 模板</span>
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">run</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>1.17.1
        <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
        <span class="token key atrule">ports</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">containerPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
          <span class="token key atrule">protocol</span><span class="token punctuation">:</span> TCP
</code></pre> 
<p>然后就可以执行对应的创建和删除命令了：</p> 
<pre><code>创建：kubectl  create  -f  deploy-nginx.yaml

删除：kubectl  delete  -f  deploy-nginx.yaml
</code></pre> 
<h3><a id="Service_682"></a>Service</h3> 
<p>通过上节课的学习，已经能够利用Deployment来创建一组Pod来提供具有高可用性的服务。</p> 
<p>虽然每个Pod都会分配一个单独的Pod IP，然而却存在如下两问题：</p> 
<ul><li>Pod IP 会随着Pod的重建产生变化</li><li>Pod IP 仅仅是集群内可见的虚拟IP，外部无法访问</li></ul> 
<p>这样对于访问这个服务带来了难度。因此，kubernetes设计了Service来解决这个问题。</p> 
<p>Service可以看作是一组同类Pod<strong>对外的访问接口</strong>。借助Service，应用可以方便地实现服务发现和负载均衡。</p> 
<p><img src="https://i-blog.csdnimg.cn/blog_migrate/460b1bbf8d257989299f55be99df0627.png" alt="Service"></p> 
<p><strong>操作一：创建集群内部可访问的Service</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 暴露Service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl expose deploy nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev</span>
service/svc-nginx1 exposed

<span class="token comment"># 查看service</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get svc svc-nginx1 -n dev -o wide</span>
NAME         <span class="token function">TYPE</span>        CLUSTER-IP       EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>   AGE     SELECTOR
svc-nginx1   ClusterIP   10<span class="token punctuation">.</span>109<span class="token punctuation">.</span>179<span class="token punctuation">.</span>231   &lt;none&gt;        80/TCP    3m51s   run=nginx

<span class="token comment"># 这里产生了一个CLUSTER-IP，这就是service的IP，在Service的生命周期中，这个地址是不会变动的</span>
<span class="token comment"># 可以通过这个IP访问当前service对应的POD</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># curl 10.109.179.231:80</span>
&lt;<span class="token operator">!</span>DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Welcome to nginx!&lt;<span class="token operator">/</span>title&gt;
&lt;<span class="token operator">/</span>head&gt;
&lt;body&gt;
&lt;h1&gt;Welcome to nginx!&lt;<span class="token operator">/</span>h1&gt;
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
&lt;<span class="token operator">/</span>body&gt;
&lt;<span class="token operator">/</span>html&gt;
</code></pre> 
<p><strong>操作二：创建集群外部也可访问的Service</strong></p> 
<pre><code class="prism language-powershell"><span class="token comment"># 上面创建的Service的type类型为ClusterIP，这个ip地址只用集群内部可访问</span>
<span class="token comment"># 如果需要创建外部也可以访问的Service，需要修改type为NodePort</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev</span>
service/svc-nginx2 exposed

<span class="token comment"># 此时查看，会发现出现了NodePort类型的Service，而且有一对Port（80:31928/TC）</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl get svc  svc-nginx2  -n dev -o wide</span>
NAME          <span class="token function">TYPE</span>        CLUSTER-IP       EXTERNAL-IP   PORT<span class="token punctuation">(</span>S<span class="token punctuation">)</span>        AGE    SELECTOR
svc-nginx2    NodePort    10<span class="token punctuation">.</span>100<span class="token punctuation">.</span>94<span class="token punctuation">.</span>0      &lt;none&gt;        80:31928/TCP   9s     run=nginx

<span class="token comment"># 接下来就可以通过集群外的主机访问 节点IP:31928访问服务了</span>
<span class="token comment"># 例如在的电脑主机上通过浏览器访问下面的地址</span>
http:<span class="token operator">/</span><span class="token operator">/</span>192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>100:31928/
</code></pre> 
<p><strong>删除Service</strong></p> 
<pre><code class="prism language-powershell"><span class="token namespace">[root@master ~]</span><span class="token comment"># kubectl delete svc svc-nginx-1 -n dev                                   service "svc-nginx-1" deleted</span>
</code></pre> 
<p><strong>配置方式</strong></p> 
<p>创建一个svc-nginx.yaml，内容如下：</p> 
<pre><code class="prism language-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> svc<span class="token punctuation">-</span>nginx
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">clusterIP</span><span class="token punctuation">:</span> 10.109.179.231
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
    <span class="token key atrule">protocol</span><span class="token punctuation">:</span> TCP
    <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">80</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">run</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">type</span><span class="token punctuation">:</span> ClusterIP
</code></pre> 
<p>然后就可以执行对应的创建和删除命令了：</p> 
<pre><code>创建：kubectl  create  -f  svc-nginx.yaml

删除：kubectl  delete  -f  svc-nginx.yaml
</code></pre> 
<h3><a id="EndPoint_775"></a>EndPoint</h3> 
<p>Endpoint是kubernetes中的一个资源对象，存储在etcd中，用来记录一个service对应的所有pod的访问地址，它是根据service配置文件中selector描述产生的。</p> 
<p>一个Service由一组Pod组成，这些Pod通过Endpoints暴露出来，Endpoints是实现实际服务的端点集合。换句话说，service和pod之间的联系是通过endpoints实现的。</p> 
<p>每创建一个service，k8s会自动创建一个同名的 Endpoint出来<br> 1、查看 endpoint</p> 
<pre><code># 一定要加s
kubectl get endpoints -n dev

# 查看endpoint的详细信息
kubectl describe endpoints -n dev
</code></pre> 
<p>2、删除endpoint</p> 
<pre><code>kubectl delete endpoints xxxx -n dev
</code></pre> 
<p>3、创建endpoint<br> 因为每次创建一个service，k8s会自动创建一个同名的 Endpoint出来，所我们直接创建service就可以了</p> 
<pre><code>kind: Service
apiVersion: v1
metadata:
  name: nginx
  namespace: dev
spec:
  type: ClusterIP
  ports:
  - name: app-port
    protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: nginx-pod
</code></pre> 
<blockquote> 
 <p><strong>小结</strong></p> 
 <p>至此，已经掌握了Namespace、Pod、Deployment、Service资源的基本操作，有了这些操作，就可以在kubernetes集群中实现一个服务的简单部署和访问了，但是如果想要更好的使用kubernetes，就需要深入学习这几种资源的细节和原理。</p> 
</blockquote>
                </div>
            </body>
            </html>
            